import { Body, Controller, Post } from '@nestjs/common';
import { EmailService } from './email.service';
import { RedisService } from 'src/redis/redis.service';
import { CreateEmailDto } from './dto/create-email.dto';

@Controller('email')
export class EmailController {
  constructor(
    private readonly emailService: EmailService,
    private readonly redisService: RedisService,
  ) {}

  @Post()
  async sendEmail(@Body('email') email: string) {
    const code = Math.random().toString().slice(2, 8);
    await this.redisService.set(email, code, 60 * 5);
    await this.emailService.sendEmail({
      to: email,
      subject: 'Testing NestJS Email Module',
      html: `<h1>${code}</h1>`,
    });

    return { message: 'Email sent successfully' };
  }

  @Post('login')
  async login(@Body() createEmailDto: CreateEmailDto) {
    return this.emailService.login(createEmailDto);
  }
}
